<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item :label="$t('columns.host')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="host">
              <a-input v-model="model.host" :placeholder="$t('common.form.rule.input', {label: $t('columns.host')})"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('columns.port')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="port">
              <a-input v-model="model.port" :placeholder="$t('common.form.rule.input', {label: $t('columns.port')})"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('columns.username')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="username">
              <a-input v-model="model.username" rows="4" :placeholder="$t('common.form.rule.input', {label: $t('columns.username')})" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('columns.password')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="password">
              <a-input v-model="model.password" rows="4" :placeholder="$t('common.form.rule.input', {label: $t('columns.password')})" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('columns.useAuth')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="useAuth">
              <a-switch v-model="model.useAuth" :checked-children="$t('dict.yesNo.1')" :un-checked-children="$t('dict.yesNo.0')" default-checked />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('columns.starttlsEnabled')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="starttlsEnabled">
              <a-switch v-model="model.starttlsEnabled" :checked-children="$t('dict.yesNo.1')" :un-checked-children="$t('dict.yesNo.0')" default-checked />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<i18n>
  {
    "zh-CN": {
      "columns": {
        "host": "SMTP服务器地址",
        "port": "SMTP服务器端口",
        "username": "SMTP服务器账户",
        "password": "SMTP服务器密码",
        "useAuth": "是否需要身份验证",
        "starttlsEnabled": "是否启用STARTTLS"
      },
      "dict": {
        "yesNo": {
          "0": "否",
          "1": "是"
        }
      }
    },
    "en-US": {
      "columns": {
        "host": "SMTP server address",
        "port": "SMTP server port",
        "username": "SMTP server username",
        "password": "SMTP server password",
        "useAuth": "need authentication",
        "starttlsEnabled": "enable STARTTLS"
      },
      "dict": {
        "yesNo": {
          "0": "NO",
          "1": "YES"
        }
      }
    },
    "fr-FR": {
      "columns": {
        "host": "adresse du serveur SMTP",
        "port": "port du serveur SMTP",
        "username": "mot de username du serveur SMTP",
        "password": "mot de passe du serveur SMTP",
        "useAuth":  "Authentification requise",
        "starttlsEnabled":  "Activer starttls"
      },
      "dict": {
        "yesNo": {
          "0": "Non",
          "1": "Oui"
        }
      }
    }
  }
</i18n>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'MessageEmailConfigForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/message/email-config/add",
          edit: "/message/email-config/edit",
          queryById: "/message/email-config/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>